% Class representing the animatronic turtled named Franklin
% 
% Written by: Eric Lam
% May 2013
classdef Franklin
   properties(Constant)
       CHANNEL_IR = 5;
       CHANNEL_RETRACT = 0;
       CHANNEL_LEGS = 1;
       SPEED_EXPAND_ARMS_HEAD = 5;
       SPEED_EXPAND_LEGS = 5;
       SPEED_RETRACT_ARMS_HEAD = 5;
       SPEED_RETRACT_LEGS = 5;
   end
   
   properties(SetAccess = 'private', GetAccess = 'public')
       controller;
   end
   
   methods
       % Constructor.
       function obj = Franklin(comPortNumber)
           delete(instrfind);
           obj.controller = ServoController(comPortNumber);
           obj.controller.setSpeed(obj.CHANNEL_RETRACT, obj.SPEED_RETRACT_ARMS_HEAD);
           obj.controller.setSpeed(obj.CHANNEL_LEGS, obj.SPEED_RETRACT_LEGS);
           obj.expand();
       end

       function retract(obj)
           obj.controller.setSpeed(obj.CHANNEL_RETRACT, obj.SPEED_RETRACT_ARMS_HEAD);
           obj.controller.setSpeed(obj.CHANNEL_LEGS, obj.SPEED_RETRACT_LEGS);
           obj.controller.setPosition(obj.CHANNEL_RETRACT, 188);
           obj.moveLegsIn();
       end
       
       function expand(obj)
           obj.controller.setSpeed(obj.CHANNEL_RETRACT, obj.SPEED_EXPAND_ARMS_HEAD);
           obj.controller.setSpeed(obj.CHANNEL_LEGS, obj.SPEED_EXPAND_LEGS);
           obj.controller.setPosition(obj.CHANNEL_RETRACT, 68);
           obj.moveLegsOut();
       end
       
       function moveLegsOut(obj)
           obj.controller.setPosition(obj.CHANNEL_LEGS, 0);
       end
       
       function moveLegsIn(obj)
           obj.controller.setPosition(obj.CHANNEL_LEGS, 254);
       end
       
       % Is the ultrasonic sensor detecting an object in certain range
       function isInRange = isInRange(obj, threshold)
           if (obj.controller.getAnalogInput(obj.CHANNEL_IR) > threshold)
               isInRange = 1;
           else
               isInRange = 0;
           end
       end
       
       function irInput = getIR(obj)
           irInput = obj.controller.getAnalogInput(obj.CHANNEL_IR);
       end
       
       function shutDown(obj)
           obj.controller.shutDown();
       end
   end
end